package wec_wallet.ebo.com.intercept;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;

import bassproject.ebo.com.ebobass.application.Phone;
import bassproject.ebo.com.ebobass.utils.Lg;
import wec_wallet.ebo.com.widget.InterceptActiDialog;

/**
 * 来去电监听
 */

public class CustomPhoneStateListener extends PhoneStateListener {

    private Context mContext;

    public CustomPhoneStateListener(Context context) {
        mContext = context;
    }

    @Override
    public void onServiceStateChanged(ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);
        Lg.d("CustomPhoneStateListener onServiceStateChanged: " + serviceState);
    }


    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:      // 电话挂断
                Lg.d("CustomPhoneStateListener state: " + state + " 空闲: ");
                break;
            case TelephonyManager.CALL_STATE_RINGING:   // 电话响铃
                Lg.d("CALL_STATE_RINGING" + incomingNumber);
                if (Phone.isHave(incomingNumber)) {
                    Intent i = new Intent(mContext, InterceptActiDialog.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.putExtra("incomingNumber", incomingNumber);
                    mContext.startActivity(i);
                }

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:    // 来电接通 或者 去电，去电接通  但是没法区分
                Lg.d("CALL_STATE_OFFHOOK" + incomingNumber);
                break;
        }
    }

}